home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / csetup.zip / SETUPAPI.C < prev    next >
C/C++ Source or Header  |  1993-12-23  |  9KB  |  417 lines

  1. /*
  2.     setupapi.c
  3.  
  4.     Functions which do stuff like the BASIC Setup functions
  5. */
  6.  
  7. // COPYRIGHT:
  8. //
  9. //   (C) Copyright Microsoft Corp. 1993.  All rights reserved.
  10. //
  11. //   You have a royalty-free right to use, modify, reproduce and
  12. //   distribute the Sample Files (and/or any modified version) in
  13. //   any way you find useful, provided that you agree that
  14. //   Microsoft has no warranty obligations or liability for any
  15. //   Sample Application Files which are modified.
  16. //
  17.  
  18. #include <windows.h>
  19. #include "setupapi.h"
  20. #include "csetup.h"
  21. #include <string.h>
  22.  
  23. //
  24. // Extern 
  25. //
  26.  
  27. extern CATCHBUF CatchBuf;
  28.  
  29. //
  30. // Global data 
  31. //
  32.  
  33. HANDLE hSetup;
  34. BOOL fFrameInit;
  35. BOOL fInstallInit;
  36.  
  37. //
  38. // macros
  39. //
  40.  
  41. #undef ERROR
  42. #define ERROR(e)  Throw((int FAR *)CatchBuf, (e))
  43.  
  44.  
  45. //
  46. // local functions
  47. //
  48.  
  49. static void End(void);
  50. static void StfApiErr(UINT uiMsg, LPSTR szApi, LPSTR szArgs);
  51. static HWND InitFrame(LPSTR szCmdLine);
  52. static void InitInstall(void);
  53. static void BadArgErr(UINT uiArg, LPSTR szApi, LPSTR szArgs);
  54. static BOOL FValidInfSect(LPSTR szSect);
  55. static BOOL FValidIniFile(LPSTR szFile);
  56.  
  57.  
  58. //
  59. // Global functions
  60. //
  61.  
  62.  
  63. BOOL CreateProgManItem(LPSTR szGroup, LPSTR szItem, LPSTR szCmd, LPSTR szOther, UINT cmo)
  64. {
  65.     char szItemNew[256];
  66.     char buf[256];
  67.  
  68.     if (!szOther || !lstrlen(szOther)) {
  69.         lstrcpy(szItemNew, szItem);
  70.     } else {
  71.         wsprintf(szItemNew, "%s,%s", szItem, szOther);
  72.     }
  73.     if (!szGroup
  74.     || !lstrlen(szGroup)
  75.     || (lstrlen(szGroup) > 24)) {
  76.         wsprintf(buf, "%s, %s, %s, %s, %u", szGroup, szItem, szCmd, szOther, cmo);
  77.         BadArgErr(1, "CreateProgmanItem", buf);
  78.     }
  79.  
  80.     if (FCreateProgManItem(szGroup, szItemNew, szCmd, cmo) == 0) {
  81.         wsprintf(buf, "%s, %s, %s, %s, %u", szGroup, szItem, szCmd, szOther, cmo);
  82.         StfApiErr(saeFail, "CreateProgmanItem", buf);
  83.         ERROR(STFERR);
  84.     }
  85.     return TRUE;
  86. }
  87.  
  88. BOOL ShowProgmanGroup(LPSTR szGroup, UINT uiCmd, UINT cmo)
  89. {
  90.     char buf[256];
  91.  
  92.     if (!lstrlen(szGroup)
  93.     || lstrlen(szGroup) > 24) {
  94.         BadArgErr(1, "ShowProgmanGroup", szGroup);
  95.     }
  96.     wsprintf(buf, "%u%", uiCmd);
  97.     if (FShowProgManGroup(szGroup, buf, cmo) == 0) {
  98.         wsprintf(buf, "%s, %u, %u", szGroup, uiCmd, cmo);
  99.         StfApiErr(saeFail, "ShowProgmanGroup", buf);
  100.         ERROR(STFERR);
  101.     }
  102.     return TRUE;
  103. }
  104.  
  105.  
  106. void CreateIniKeyValue(LPSTR szFile, LPSTR szSect, LPSTR szKey, 
  107.                        LPSTR szValue, UINT cmo)
  108. {
  109.     UINT n;
  110.     char buf[256];
  111.  
  112.     if (FValidIniFile(szFile) == 0) {
  113.         n = 1;
  114.     } else if (FValidInfSect(szSect) == 0) {
  115.         n = 2;
  116.     } else {
  117.         n = 0;
  118.     }
  119.     if (n > 0) {
  120.         wsprintf(buf, "%s, %s, %s, %s, %y", szFile, szSect, szKey, szValue, cmo);
  121.         BadArgErr(n, "CreateIniKeyValue", buf);
  122.     }
  123.  
  124.     if (FCreateIniKeyValue(szFile, szSect, szKey, szValue, cmo) == 0) {
  125.         wsprintf(buf, "%s, %s, %s, %s, %y", szFile, szSect, szKey, szValue, cmo);
  126.         StfApiErr(saeFail, "CreateIniKeyValue", buf);
  127.         ERROR(STFERR);
  128.     }
  129. }
  130.  
  131.  
  132. HANDLE InitSetup(LPSTR szCmdLine)
  133. {
  134.     HANDLE h;
  135.  
  136.     fFrameInit = FALSE;
  137.     fInstallInit = FALSE;
  138.  
  139.     if (hSetup) {
  140.         StfApiErr(saeInit, "InitSetup", szCmdLine);
  141.     }
  142.  
  143.     h = InitFrame(szCmdLine);
  144.     fFrameInit = TRUE;
  145.  
  146.     InitInstall();
  147.     fInstallInit = TRUE;
  148.  
  149.     return h;
  150. }
  151.  
  152. void ReadInfFile(LPSTR lpszInfFile)
  153. {
  154.     if (FOpenInf(lpszInfFile, 1, 0) == 0) {
  155.         Error("Failed to read %s", lpszInfFile);
  156.     }
  157. }
  158.  
  159. UINT UIStartDlg(LPSTR szDll,
  160.                 UINT uiDlg, 
  161.                 LPSTR szDlgProc,
  162.                 UINT uiHelpDlg, 
  163.                 LPSTR szHelpProc)
  164. {
  165.     char sz[MAXSTRINGLENGTH];
  166.  
  167.     FDoDialog(HwndFrame(), szDll, uiDlg, szDlgProc, uiHelpDlg, szHelpProc);
  168.     GetSymbolValue("DLGEVENT", sz, sizeof(sz));
  169.     if (lstrcmp(sz, "EXIT") == 0) {
  170.         return EXIT;
  171.     } else if (lstrcmp(sz, "REACTIVATE") == 0) {
  172.         return REACTIVATE;
  173.     } else if (lstrcmp(sz, "CONTINUE") == 0) {
  174.         return CONTINUE;
  175.     } else if (lstrcmp(sz, "BACK") == 0) {
  176.         return BACK;
  177.     } else {
  178.         return CONTINUE;
  179.     }
  180. }
  181.  
  182.  
  183. UINT AskQuit()
  184. {
  185.     do {
  186.  
  187.         switch (UIStartDlg(szCUIDLL, ASKQUIT, "FQuitDlgProc", APPHELP, "FHelpDlgProc")) {
  188.         case EXIT:
  189.             UIPopAll;
  190. //          return EXIT;
  191.             Throw((int FAR *)CatchBuf, STFQUIT);
  192.  
  193.         case CONTINUE:
  194.             UIPop(1);
  195.             return CONTINUE;
  196.  
  197.         case REACTIVATE:
  198.         default:
  199.             break;
  200.         }
  201.     } while(1);
  202. }
  203.  
  204. void BadPath()
  205. {
  206.     do {
  207.  
  208.         if (UIStartDlg(szCUIDLL, BADPATH, "FInfo0DlgProc", 0, "") != REACTIVATE) {
  209.             UIPop(1);
  210.             return;
  211.         }
  212.     } while(1);
  213. }
  214.  
  215. void UIPop(UINT uiCount)
  216. {
  217.     FKillNDialogs(uiCount);
  218. }
  219.  
  220.  
  221. void UIPopAll()
  222. {
  223.     UIPop((UINT)65535);
  224. }
  225.  
  226. //
  227. // Show an error message box
  228. //
  229.  
  230. void cdecl Error(LPSTR lpFormat, ...) 
  231. {
  232.     char buf[256];
  233.  
  234.     wvsprintf(buf, lpFormat, (LPSTR)(&lpFormat+1));
  235.     MessageBeep(MB_ICONEXCLAMATION);
  236.     MessageBox(HwndFrame(), buf, szAppName, MB_OK|MB_ICONEXCLAMATION);
  237. }
  238.  
  239. void MakePath(LPSTR szDir, LPSTR szFile, LPSTR szPath)
  240. {
  241.     if (!szDir || !lstrlen(szDir)) {
  242.         lstrcpy(szPath, szFile);
  243.     } else if (!szFile || !lstrlen(szFile)) {
  244.         lstrcpy(szPath, szDir);
  245.     } else if (szDir[lstrlen(szDir)-1] == '\\') {
  246.         lstrcpy(szPath, szDir);
  247.         lstrcat(szPath, szFile);
  248.     } else {
  249.         lstrcpy(szPath, szDir);
  250.         lstrcat(szPath, "\\");
  251.         lstrcat(szPath, szFile);
  252.     }
  253. }
  254.  
  255. void cdecl WriteToLogFile(LPSTR lpFormat, ...) 
  256. {
  257.     char buf[256];
  258.  
  259.     wvsprintf(buf, lpFormat, (LPSTR)(&lpFormat+1));
  260.     FWriteToLogFile(buf, 1);
  261. }
  262.  
  263. BOOL AddSectionFilesToCopyList(LPSTR szSect, LPSTR szSrc, LPSTR szDest)
  264. {
  265.     int n;
  266.     char buf[256];
  267.  
  268.     if (FValidInfSect(szSect) == 0) {
  269.         n = 1;
  270.     } else if (FValidFATDir(szSrc) == 0) {
  271.         n = 2;
  272.     } else if (FValidFATDir(szDest) == 0) {
  273.         n = 3;
  274.     } else {
  275.         n = 0;
  276.     }
  277.     if (n > 0) {
  278.         wsprintf(buf, "%s, %s, %s", szSect, szSrc, szDest);
  279.         BadArgErr(n, "AddSectionFilesToCopyList", buf);
  280.     }
  281.     if (FAddSectionFilesToCopyList(szSect, szSrc, szDest) == 0) {
  282.         wsprintf(buf, "%s, %s, %s", szSect, szSrc, szDest);
  283.         StfApiErr(saeFail, "AddSectionFilesToCopyList", buf);
  284.         ERROR(STFERR);
  285.     }
  286.     return TRUE;
  287. }
  288.  
  289. UINT CopyFilesInCopyList(void)
  290. {
  291.     return GrcCopyFilesInCopyList(HinstFrame());
  292. }
  293.  
  294. void FAR cdecl DbgOut(LPSTR lpFormat, ...) 
  295. {
  296.     char buf[256];
  297.  
  298.     if (!GetSystemMetrics(SM_DEBUG)) return;
  299.  
  300.     wsprintf(buf, "%s: ", (LPSTR)szAppName);
  301.     OutputDebugString(buf);
  302.     wvsprintf(buf, lpFormat, (LPSTR)(&lpFormat+1));
  303.     OutputDebugString(buf);
  304.     OutputDebugString("\r\n");
  305. }
  306.  
  307. void CreateDir(LPSTR szDir, UINT uiCmd)
  308. {
  309.     char buf[256];
  310.  
  311.     if (FValidFATDir(szDir) == 0) {
  312.         wsprintf(buf, "%s, %u", szDir, uiCmd);
  313.         BadArgErr(1, "CreateDir", buf);
  314.     }
  315.     if (FCreateDir(szDir, uiCmd) == 0) {
  316.         wsprintf(buf, "%s, %u", szDir, uiCmd);
  317.         StfApiErr(saeFail, "CreateDir", buf);
  318.         ERROR(STFERR);
  319.     }
  320. }
  321.  
  322. //
  323. // Local functions
  324. //
  325.  
  326. static void End(void)
  327. {
  328.     ERROR(STFQUIT);
  329. }
  330.  
  331. static void StfApiErr(UINT uiMsg, LPSTR szApi, LPSTR szArgs)
  332. {
  333.     LPSTR lpText;
  334.     char msg[32];
  335.     char buf[256];
  336.  
  337.     switch (uiMsg) {
  338.     case saeFail:
  339.         lpText = "Failed";
  340.         break;
  341.  
  342.     case saeInit:
  343.         lpText = "Already Initialized";
  344.         break;
  345.  
  346.     case saeNYI:
  347.         lpText = "NYI"; // not yet initialized?
  348.         break;
  349.  
  350.     default:
  351.         wsprintf(msg, "Bad Arg %u", uiMsg - saeArg);
  352.         lpText = msg;
  353.         break;
  354.     }
  355.  
  356.     if (szArgs && lstrlen(szArgs)) {
  357.         wsprintf(buf, "%s: %s (%s)", lpText, szApi, szArgs);
  358.     } else {
  359.         wsprintf(buf, "%s: %s", lpText, szApi);
  360.     }
  361.     MessageBox(HwndFrame(), buf, "C-Setup API Error", MB_TASKMODAL | MB_ICONHAND | MB_OK);
  362.     End();
  363. }
  364.  
  365. static void BadArgErr(UINT uiArg, LPSTR szApi, LPSTR szArgs)
  366. {
  367.     StfApiErr(uiArg+saeArg, szApi, szArgs);
  368.     ERROR(STFERR);
  369. }
  370.  
  371. static HWND InitFrame(LPSTR szCmdLine)
  372. {
  373.     HWND hWnd;
  374.  
  375.     if (hSetup) {
  376.         StfApiErr(saeInit, "InitFrame", szCmdLine);
  377.     } else {
  378.         hWnd = InitializeFrame(szCmdLine);
  379.         if ((int)hWnd == -1) {
  380.             End(); // not an error
  381.         } else if (hWnd == NULL) {
  382.             StfApiErr(saeFail, "InitFrame", szCmdLine);
  383.             End();
  384.         } else {
  385.             return hWnd;
  386.         }
  387.     }
  388. }
  389.  
  390. static void InitInstall(void)
  391. {
  392.     if (hSetup) {
  393.         StfApiErr(saeInit, "InitInstall", "");
  394.     } else if (FInitializeInstall(HinstFrame(), HwndFrame()) == 0) {
  395.         StfApiErr(saeFail, "InitInstall", "");
  396.     }
  397. }
  398.  
  399. static BOOL FValidInfSect(LPSTR szSect)
  400. {
  401.     if (!szSect 
  402.     || !lstrlen(szSect)
  403.     || (_fstrchr(szSect,']') != NULL)) {
  404.         return FALSE;
  405.     }
  406.     return TRUE;
  407. }
  408.  
  409. static BOOL FValidIniFile(LPSTR szFile)
  410. {
  411.     if ((FValidFATPath(szFile) == 0)
  412.     && (_fstricmp(szFile, "WIN.INI") != 0)) {
  413.         return FALSE;
  414.     }
  415.     return TRUE;
  416. }
  417.